package com.travel.travelServer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.travel.travelServer.entity.*;
import com.travel.travelServer.mapper.*;
import com.travel.travelServer.service.GoodsService;
import com.travel.travelServer.utils.BaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (Fruit)表服务实现类
 *
 * @author makejava
 * @since 2024-03-01 15:39:05
 */
@Service("fruitService")
public class GoodsServiceImpl extends BaseService<GoodsMapper, Goods> implements GoodsService {

    @Resource
    private BusinessMapper categoryDao;

    @Resource
    private GoodsMapper fruitDao;

    @Resource
    private CarMapper carDao;

    @Resource
    private CollectMapper collectDao;

    @Resource
    private StoreFruitMapper storeFruitMapper;

    @Override
    public Page<Goods> page(Page<Goods> page, Map<String, Object> conditions) {
        LambdaQueryWrapper<Goods> fruitLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fruitLambdaQueryWrapper.like(Objects.nonNull(conditions.get("name")) && conditions.get("name") != "", Goods::getName, conditions.get("name"));
        Page<Goods> fruitPage = super.page(page, fruitLambdaQueryWrapper);
        fruitPage.getRecords().stream().forEach(fruit -> {
            if (Objects.nonNull(fruit.getCategoryId())){
                fruit.setCategory(categoryDao.selectById(fruit.getCategoryId()));
            }
        });
        return fruitPage;
    }

    @Override
    public Goods getById(Long id) {
        Goods goods = super.getById(id);
        goods.setCategory(categoryDao.selectById(goods.getCategoryId()));
        return goods;
    }

    @Override
    public Boolean add(Goods pojo) {
        return super.save(pojo);
    }

    @Override
    public Boolean update(Goods pojo) {
        return super.updateById(pojo);
    }

    @Override
    public Boolean removeOne(Long id) {
        return super.removeById(id);
    }

    @Override
    public Boolean batchRemove(List<Long> asList) {
        return super.removeByIds(asList);
    }

    @Override
    public List<Goods> getByCategory(Long id) {
        Long userId = getCurrentUser().getId();
        LambdaQueryWrapper<Goods> fruitLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fruitLambdaQueryWrapper.eq(Goods::getCategoryId, id);
        List<Goods> goods = fruitDao.selectList(fruitLambdaQueryWrapper);
        if (goods.size() > 0){
            goods.stream().forEach(fruit -> {
                if (Objects.nonNull(fruit.getCategoryId())){
                    fruit.setCategory(categoryDao.selectById(fruit.getCategoryId()));
                }
                LambdaQueryWrapper<Car> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Car::getUserId, userId);
                wrapper.eq(Car::getFruitId, fruit.getId());
                wrapper.eq(Car::getStatus, 0);
                fruit.setCount(Objects.nonNull(carDao.selectOne(wrapper)) ? carDao.selectOne(wrapper).getNumber() : 0);
                LambdaQueryWrapper<Collect> collectLambdaQueryWrapper = new LambdaQueryWrapper<>();
                collectLambdaQueryWrapper.eq(Collect::getUserId, userId);
                collectLambdaQueryWrapper.eq(Collect::getCollectId, fruit.getId());
                Collect collect = collectDao.selectOne(collectLambdaQueryWrapper);
                if (Objects.nonNull(collect)){
                    fruit.setIsCollect(false);
                }else {
                    fruit.setIsCollect(true);
                }
            });
        }
        return goods;
    }

    @Override
    public List<Goods> getMyGoods(Page<Goods> page, Map<String, Object> conditions) {
        Long userId = getCurrentUser().getId();
        QueryWrapper<StoreFruit> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(StoreFruit::getUserId, userId);
        wrapper.select("DISTINCT fruit_id");
        List<StoreFruit> storeFruits = storeFruitMapper.selectList(wrapper);
        List<Goods> goods = new ArrayList<>();
        storeFruits.stream().forEach(storeCategory -> {
            goods.add(fruitDao.selectById(storeCategory.getFruitId()));
        });
        return goods;
    }
}

